import { calculateWuxing, getZodiacByYear, getWuxingByTime, WUXING_RELATIONS } from './chineseCulture';
import { getRandomCharacter, getNameMeaning, getGenderSpecificCharacter } from '@/data/nameDatabase';

// 用户输入信息接口
export interface UserInput {
  birthDate: string;
  timePeriod?: string;
  gender: '男孩' | '女孩';
  surname: string;
}

// 生成的名字结果接口
export interface GeneratedName {
  fullName: string;
  meaning: string;
  wuxing: string;
  zodiac: string;
  analysis: string;
  score: number;
}

// 名字生成器类
export class NameGenerator {
  private userInput: UserInput;

  constructor(input: UserInput) {
    this.userInput = input;
  }

  // 分析用户输入信息
  private analyzeInput() {
    const birthDate = new Date(this.userInput.birthDate);
    const year = birthDate.getFullYear();
    const month = birthDate.getMonth() + 1;
    const day = birthDate.getDate();

    const wuxing = calculateWuxing(year, month, day);
    const zodiac = getZodiacByYear(year);
    const timeWuxing = this.userInput.timePeriod ? getWuxingByTime(this.userInput.timePeriod) : wuxing;

    return {
      year,
      month,
      day,
      wuxing,
      zodiac,
      timeWuxing
    };
  }

  // 生成单个名字 - 改进版本，体现性别差异
  private generateSingleName(analysisData: ReturnType<typeof this.analyzeInput>): GeneratedName {
    // 根据五行选择合适的字符
    const primaryWuxing = analysisData.wuxing;
    const secondaryWuxing = WUXING_RELATIONS[primaryWuxing as keyof typeof WUXING_RELATIONS] || primaryWuxing;
    
    // 生成名字字符 - 使用性别差异化的字符库
    const firstChar = getRandomCharacter(primaryWuxing, this.userInput.gender);
    const secondChar = getRandomCharacter(secondaryWuxing, this.userInput.gender);
    
    // 组合完整名字
    const fullName = this.userInput.surname + firstChar + secondChar;
    
    // 生成寓意 - 更详细的性别差异化寓意
    const meaning = getNameMeaning(this.userInput.gender, primaryWuxing);
    
    // 生成分析说明 - 体现性别特征
    const analysisText = this.generateAnalysis(analysisData, primaryWuxing, secondaryWuxing);
    
    // 计算评分 - 考虑性别因素
    const score = this.calculateScore(analysisData, fullName);
    
    return {
      fullName,
      meaning,
      wuxing: primaryWuxing,
      zodiac: analysisData.zodiac,
      analysis: analysisText,
      score
    };
  }

  // 生成分析说明 - 改进版本，更好地体现性别差异
  private generateAnalysis(
    analysisData: ReturnType<typeof this.analyzeInput>,
    primaryWuxing: string,
    secondaryWuxing: string
  ): string {
    const { zodiac, year } = analysisData;
    const gender = this.userInput.gender;
    
    let analysisText = `根据${year}年出生，生肖为${zodiac}，`;
    analysisText += `五行主${primaryWuxing}，辅以${secondaryWuxing}，`;
    
    // 根据性别生成不同的分析
    if (gender === '男孩') {
      analysisText += `体现阳刚之气，`;
      analysisText += `寓意坚强勇敢，`;
      analysisText += `音韵雄浑有力，字形刚劲挺拔。`;
    } else {
      analysisText += `体现温婉之美，`;
      analysisText += `寓意聪慧优雅，`;
      analysisText += `音韵柔美动听，字形秀丽端庄。`;
    }
    
    if (this.userInput.timePeriod) {
      analysisText += `结合${this.userInput.timePeriod}的五行特点，`;
    }
    
    analysisText += `整体平衡和谐，符合传统文化理念。`;
    
    return analysisText;
  }

  // 计算名字评分 - 改进版本，考虑性别因素
  private calculateScore(analysisData: ReturnType<typeof this.analyzeInput>, fullName: string): number {
    let score = 80; // 基础分
    
    // 根据五行搭配加分
    const primaryWuxing = analysisData.wuxing;
    const secondaryWuxing = WUXING_RELATIONS[primaryWuxing as keyof typeof WUXING_RELATIONS];
    if (secondaryWuxing) {
      score += 10; // 五行相生加分
    }
    
    // 根据名字长度调整
    if (fullName.length === 3) {
      score += 5; // 三字名加分
    }
    
    // 根据生肖特点加分
    const luckyZodiacs = ['龙', '虎', '马', '猴'];
    if (luckyZodiacs.includes(analysisData.zodiac)) {
      score += 5;
    }
    
    // 根据性别特征加分
    if (this.userInput.gender === '男孩') {
      // 男孩名字应该体现阳刚之气
      const masculineChars = ['伟', '强', '勇', '刚', '毅', '志', '豪', '杰', '雄', '威'];
      if (masculineChars.some(char => fullName.includes(char))) {
        score += 5;
      }
    } else {
      // 女孩名字应该体现温婉之美
      const feminineChars = ['美', '丽', '雅', '静', '婷', '娜', '芳', '芬', '香', '馨'];
      if (feminineChars.some(char => fullName.includes(char))) {
        score += 5;
      }
    }
    
    return Math.min(score, 100); // 最高100分
  }

  // 生成多个名字推荐 - 确保性别差异化
  public generateNames(count: number = 3): GeneratedName[] {
    const analysisData = this.analyzeInput();
    const names: GeneratedName[] = [];
    
    for (let i = 0; i < count; i++) {
      const name = this.generateSingleName(analysisData);
      names.push(name);
    }
    
    // 按评分排序
    return names.sort((a, b) => b.score - a.score);
  }

  // 重新生成名字
  public regenerateNames(count: number = 3): GeneratedName[] {
    return this.generateNames(count);
  }
}

// 便捷函数：直接生成名字
export function generateNames(input: UserInput, count: number = 3): GeneratedName[] {
  const generator = new NameGenerator(input);
  return generator.generateNames(count);
} 